﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace RESTful.DAO
{
    public class DanhMuc_DAO : DataProvider
    {
        public DanhMuc_DAO()
        {
            Connect();
        }

        public object ConvertDataTableToArray(DataTable dt, int i)
        {
            DTO.DanhMuc_DTO data = new DTO.DanhMuc_DTO();
            data.MaDanhMuc = Convert.ToInt32(dt.Rows[i]["MaDanhMuc"].ToString());
            data.TenDanhMuc = dt.Rows[i]["TenDanhMuc"].ToString();                 
            return (object)data;
        }

        public DTO.DanhMuc_DTO[] GetCategory()
        {
            try
            {
                String sql = "Select * From DanhMuc Where TinhTrang = 1";                
                DataTable dt = ExecuteQuery(sql);
                DTO.DanhMuc_DTO[] ds = new DTO.DanhMuc_DTO[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                    ds[i] = (DTO.DanhMuc_DTO)ConvertDataTableToArray(dt, i);               
                return ds;
            }
            catch
            {
                DisConnect();
                return null;
            }
        }
    }
}